var MakeDataHandel = {
	now :new Date().Format("yyyy-MM-dd"),
	date30: new Date(new Date().setDate(new Date().getDate() - 365)).Format("yyyy-MM-dd"),
	listIdx:0,
	getData:function(list,callBack){
		this.listIdx = this.listIdx>list.length-1?0:this.listIdx;
		let item = list[this.listIdx];
	
		this.listIdx++;
		this.makeDays(item,()=>{//获取一年数据
			this.makePrice(item,()=>{//获取当日数据
				this.buySellHandle(item);//获取买卖数据
				this.makeDayPersent(item);//获取某日到现在的涨跌幅度
				this.makeDayFlag(item);//计算某日到现在是否连续上升
				if(this.listIdx<list.length){
					this.getData(list,callBack);//递归
				}else{
					this.listIdx=0;
					callBack();
					
				}
			});
		});
	},
	//获取当天前一年的日K数据
	makeDays:function(item,callBack){
		if (!item.days) {
		   GuVo.getDateRang(item.code, this.date30, this.now).then(res=>{
			   if (!res.data[item.code].qfqday) {
			   	item.days = res.data[item.code].day;
			   
			   } else {
			   	item.days = res.data[item.code].qfqday;
			   
			   }
			   item.days ? item.days.reverse() : '';
			   callBack();
		   });
		}else{
			callBack();
		}
	},
	//获取当日数据
	makePrice:function(row,callBack){
		GuVo.getNowP(row.code).then(item=>{
		row.dateList = item["data"][row.code].data.data;
		let nowData = GuVo.getNowVo(row.code, item);
		Object.assign(row,nowData);
		row.total = row.price*row.stkqty;//总价值
		row.realCost = (row.cost*row.stkqty-5)/row.stkqty;//真实成本价减去5块手续费
		row.myPersent = row.cost>0?((row.price - row.cost) / row.cost * 100).toFixed(2):100;//持仓涨跌
		
		//sell*num-(sell*num*0.001)-10-buy*num;
		row.sellPrice =((row.cost*row.stkqty*0.001+5)/row.stkqty+parseFloat(row.cost)).toFixed(2);//不亏本最低卖出价
		row.realPrice=row.price*row.stkqty-(row.price*row.stkqty*0.001)-10-row.realCost*row.stkqty;//扣除所有费率的盈亏
		row.realPrice=row.realPrice.toFixed(2);
		row.total = row.total.toFixed(2);
		
		row.halfNum = (row.stkbal / 2 / 100).toFixed(0) * 100; //半仓数量
		row.halfBuyCost = ((row.halfNum * row.price * 0.001 + 10) / row.halfNum + row.price).toFixed(2); //卖半仓最低价
		row.halfSellCost = (row.price-(row.halfNum * row.price * 0.001 + 5) / row.halfNum ).toFixed(2); //卖半仓跌到多小开始小亏
		row.sellFlag=row.persent<-3?true:false;//跌3%卖出
		row.isUp = row.up && row.price > row.up;//是否高于设定最大值；
		row.isDown = row.down && row.price < row.down;//是否低于设定最小值；
		row.nowData = nowData;
		row.showChart=false;
		if(row.isDown){
			row.sell=true;
		}else{
			row.sell=false;
		}
		callBack();
		
		});
	},
	
	//买卖策略获取
	buySellHandle:function(row){
		if(row.days){
			if(row.days[0][0] == new Date().Format("yyyy-MM-dd")){
				row.days[0][1]=row.openPrice;
				row.days[0][2] = row.price;
				row.days[0][3] = row.maxPrice;
				row.days[0][4] = row.minPrice;
			}
		
		
			let dates = [];
			let datas = [];
			let days = JSON.parse(JSON.stringify(row.days));
			days = days.reverse();
			days.map(res => {
				dates.push(res.shift());
				datas.push(res);
		
			});
			let buyModel = ["buyByUpDownDay"];
			let sellModel = ["sellByDownPersent"];
			let downday=3;
			let upday = 1;
			let downPersent = 0.97;
			let threeDayDownPersent = 0;
		
			if(guStrategy[row.code]){
				if(row.code.indexOf("600048")!=-1){
					debugger;
				}
				let  rs = guStrategy[row.code];
		
				BuySell.init(rs.buyModel,rs.sellModel,rs.upday,rs.downday,rs.downPersent,rs.threeDayDownPersent,rs.topFlag,rs.minPersent,rs.maxPersent,rs.middlePersent,rs.minPrice,rs.maxPrice);
			}else{
				BuySell.init(buyModel,sellModel,upday,downday,downPersent,threeDayDownPersent,"max",0.05,0.15,0.8,0);
			}
			// debugger;
			let buySell = BuySell.bs1(datas, dates);
			let kdjList = BuySell.getKDJ(datas);
			let idx = Utils.findPeaks(kdjList.j);
		
			row.kdjFlag = kdjList.j[kdjList.j.length-1]<50;
			row.kdjVal = kdjList.j[kdjList.j.length-1];
			if(buySell.length>0){
				let lastBuySell = buySell[buySell.length-1];
				let d = new Date().Format("yyyy-MM-dd");
				if(lastBuySell.xAxis == d){
					row.buyFlag = lastBuySell.value.indexOf("B")!=-1;
					row.sellFlag=lastBuySell.value.indexOf("S")!=-1;
				}else{
					row.buyFlag=row.sellFlag=false;
				}
			
				row.buyingFlag = lastBuySell.value.indexOf("B")!=-1
				// console.log(buySell[buySell.length-1]);
				// if(row.code=="sz164824"){
				// 	row.sellFlag=true;
				// }
			}
		}
		
	},
	//计算某天到今天的升跌幅
	makeDayPersent:function(row){
		 row.dayPersent = 0;
		if (!row.days || row.days.length <row.day) return;
		let persent = (row.price - parseFloat(row.days[row.day - 1][2])) / parseFloat(row.days[row.day - 2][2]);//收盘价做标准
		 row.dayPersent = parseFloat(persent * 100).toFixed(2);
	},
	//计算前几天是否一直升
	makeDayFlag:function(row){
		let flag = true;
		for (let i = 0; i < (row.day - 1); i++) {
			let item = row.days[i];
			if (item[1] > item[2]) {
				flag = false;
				break;
			}
		}
		if (row.persent < 0) {
			flag = false;
		}
		row.flag = flag;
	}
};